/**
 * 
 */
package bai7.com.client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import bai7.com.server.StringReverseInterface;
import bai7.com.server.StringReverseRemote;

/**
 * @author nglehien Oct 21, 2012 - 10:29:53 PM
 */
public class ClientGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8965799181938840452L;
	private JPanel contentPane;
	private JTextField txtPath;
	private JFileChooser fileChooser;
	private File fileToUpload;
	private JButton btnReplace;
	private JButton btnBrowse;
	private JTextField txtFind;
	private JTextField txtReplace;
	private JTextArea textArea;
	private StringReverseInterface stringInterface;;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI frame = new ClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 264);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtPath = new JTextField();
		txtPath.setBounds(10, 12, 301, 20);
		contentPane.add(txtPath);
		txtPath.setColumns(10);

		btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(onClickBrowse);
		btnBrowse.setBounds(335, 11, 89, 23);
		contentPane.add(btnBrowse);

		btnReplace = new JButton("Replace");
		btnReplace.addActionListener(onClickUpload);
		btnReplace.setBounds(335, 38, 89, 23);
		contentPane.add(btnReplace);

		txtFind = new JTextField();
		txtFind.setColumns(10);
		txtFind.setBounds(10, 39, 142, 20);
		contentPane.add(txtFind);

		txtReplace = new JTextField();
		txtReplace.setColumns(10);
		txtReplace.setBounds(162, 39, 149, 20);
		contentPane.add(txtReplace);

		textArea = new JTextArea();
		textArea.setBounds(10, 66, 414, 149);
		contentPane.add(textArea);
	}

	private ActionListener onClickUpload = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			String s = textArea.getText();
			String s1 = txtFind.getText();
			String s2 = txtReplace.getText();
			try {
				s = stringInterface.replace(s, s1, s2);
				textArea.setText(s);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	};

	private ActionListener onClickBrowse = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			try {
				stringInterface = (StringReverseInterface) Naming
						.lookup(StringReverseRemote.REVERSE_STRING);
				fileChooser = new JFileChooser();
				int returnVal = fileChooser.showOpenDialog(ClientGUI.this);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
					fileToUpload = fileChooser.getSelectedFile();
					txtPath.setText(fileToUpload.getAbsolutePath());

					if (stringInterface
							.openFile(fileToUpload.getAbsolutePath())) {
						String data = null;
						while ((data = stringInterface.nextLine()) != null) {
							textArea.setText(textArea.getText() + data + "\n");
						}
						if (stringInterface.closeFile()) {
							textArea.setText(textArea.getText()
									+ "-----END-----");
						}
					}
				}
			} catch (NotBoundException | IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

		}
	};

}
